package com.ibiz.znjd.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.ibiz.znjd.MainApplication;
import com.ibiz.znjd.R;
import com.ibiz.znjd.znjd.GjInternal;

public class ForegroundService extends Service {
    //前台通知的id名，任意
    private String channelId = "ForegroundService";

    //前台通知的名称，任意
    private String channelName = "前台通知";

    private Handler mHandler;

    private MediaPlayer mediaPlayer;

    //服务通信
    @Nullable
    @Override
    public IBinder onBind(Intent intent){
        //与Activity进行通信
        return null;
    }

    //服务创建时
    @Override
    public void onCreate(){
        super.onCreate();
        //服务创建时创建前台通知
        Notification notification = createForegroundNotification();
        //启动前台服务
        startForeground(1,notification);
        //绑定到全局应用上
        ((MainApplication)getApplication()).foregroundService = ForegroundService.this;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    //服务销毁时
    @Override
    public void onDestroy(){
        // 销毁时清除通知
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancelAll();
        //在服务被销毁时，关闭前台服务
        stopForeground(true);
        super.onDestroy();
    }

    //创建前台通知，可写成方法体，也可单独写成一个类
    private Notification createForegroundNotification(){
        //发送通知的等级，此处为高，根据业务情况而定
        int importance = NotificationManager.IMPORTANCE_HIGH;
        // Android 8.0 及更高版本上必须先创建通知渠道，并设置重要性
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            NotificationChannel channel = new NotificationChannel(channelId,channelName,importance);
            channel.setDescription("通知渠道描述");
            NotificationManager notificationManager = (NotificationManager)getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }

        //点击通知时可进入的Activity
//        Intent notificationIntent = new Intent(this, MainActivity.class);
//        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

        //最终创建的通知，以下代码为官方写法
        //注释部分是可扩展的参数，根据自己的功能需求添加
        return new NotificationCompat.Builder(this,channelId)
                .setContentTitle("填写通知的标题")
                .setContentText("填写通知的内容")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
//                .setContentIntent(pendingIntent)//点击通知进入Activity
                .build();
        //.setOngoing(true)
        //.setPriority(NotificationCompat.PRIORITY_MAX)
        //.setCategory(Notification.CATEGORY_TRANSPORT)
        //.setLargeIcon(Icon)
        //.setWhen(System.currentTimeMillis())
    }

    public void playNosoundMedia(){
//        mediaPlayer = MediaPlayer.create(getApplicationContext(),R.raw.liveness_mouth);
//        mediaPlayer.setLooping(true);
//        if(mediaPlayer != null && !mediaPlayer.isPlaying()){
//            mediaPlayer.setOnCompletionListener(this);
//            mediaPlayer.start();
//        }
    }

    /**
     * 开启轨迹定时器，并挂载到服务上，避免被杀。
     */
    public void startInternal(){
        // 如果定时器存在删除，并重新建一个。
        if(mHandler != null){
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        // 设置定时器
        mHandler = new Handler();
        Runnable r = new Runnable() {

            @Override
            public void run() {
                GjInternal.send((MainApplication) getApplication());
                //每隔1s循环执行run方法
                mHandler.postDelayed(this, 1000*60*10);
            }
        };
        mHandler.postDelayed(r, 100);//延时100毫秒
    }
}
